precision highp float;

uniform sampler2D u_src;
uniform vec4 u_src_border;
uniform vec4 u_dst_border;

varying vec2 v_uv;


vec2 transform9slice (vec2 Slt, vec2 Srb, vec2 Dlt, vec2 Drb, vec2 Duv) {
    const vec2 C_0 = vec2(0.0);
    const vec2 C_1 = vec2(1.0);
    Drb = C_1 - Drb;
    Srb = C_1 - Srb;
    vec2 Din0 = step(Duv, Dlt);
    vec2 Din1 = step(Dlt, Duv) * step(Duv, Drb);
    vec2 Din2 = step(Drb, Duv);
    vec2 Suv0 = mix(C_0, Slt, Duv / Dlt);
    vec2 Suv1 = mix(Slt, Srb, (Duv - Dlt) / (Drb - Dlt));
    vec2 Suv2 = mix(Srb, C_1, (Duv - Drb) / (C_1 - Drb));
    return Suv0 * Din0 + Suv1 * Din1 + Suv2 * Din2;
}

void main () {
    vec2 uv = transform9slice(u_src_border.xy, u_src_border.zw, u_dst_border.xy, u_dst_border.zw, v_uv);
    gl_FragColor = texture2D(u_src, uv);
}
